import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { Message } from 'element-ui'
Vue.use(VueRouter)
const routes = [
{
	path: '/',
	name: 'home',
	component: HomeView,
	redirect: '/user/info',
	children: [
		{
			path: '/user/info',
			name: 'info',
			component: () => import('../views/user/info')
		},
		{
			path: '/user/case',
			name: 'case',
			component: () => import('../views/user/case')
		},
		{
			path: '/staff',
			name: 'staff',
			component: () => import('../views/staff')
		},
		{
			path: '/patient/info',
			name: 'patientInfo',
			component: () => import('../views/patient/info')
		},
		{
			path: '/patient/cases',
			name: 'patientCases',
			component: () => import('../views/patient/cases')
		},
		{
			path: '/system/role',
			name: 'rolems',
			component: () => import('../views/system/role')
		},
		{
			path: '/case/statistics',
			name: 'statisitcs',
			component: () => import('../views/case/statistics')
		},
		{
			path: '/case/analyse',
			name: 'analyse',
			component: () => import('../views/case/analyse')
		},
	]
},
{
	path: '/login',
	name: 'login',
	component: () => import('../views/login')
},
{
	path:'/register',
	name:'register',
	component: () => import("../views/register")
}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

router.beforeEach((to, from, next) => {
	const token = sessionStorage.getItem('token')
	if (to.path === '/login') {
		if (token) {
			Message.info('您已登录，不需要重复登录')
			return next('/')
		} else {
			return next()
		}
	}else if(to.path === '/register'){
		return next()
	} else {
		if (token) {
			return next()
		} else {
			Message.info('请登录后访问系统！')
			return next('/login')
		}
	}
})

// 防止连续点击多次路由报错
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(err => err)
}

export default router
